import axios from 'axios'
import { Message } from 'iview'

class HttpRequest {
    constructor(baseUrl = baseURL) {
        this.baseUrl = baseUrl
        this.queue = {}
    }
    getInsideConfig () {
        const config = {
            baseURL: this.baseUrl,
            headers: {
                "token": "123456",
                "content-type": "application/json;charset=UTF-8"
                //
            }
        }
        return config
    }
    destroy (url) {
        delete this.queue[url]
        if (!Object.keys(this.queue).length) {
            // Spin.hide()
        }
    }
    codeMethod (code, data, then) {
        switch (code) {
            case 400:
                Message.error({ content: data.msg, duration: 5, closable: true })
                break;
            case 401: {
                Message.warning({ content: '未授权，请重新登录', duration: 5, closable: true })
                // window.location.href = "/401"
            }
            case 403:
                Message.error({ content: '拒绝访问', duration: 5, closable: true })
                break;
            case 404:
                Message.info({ content: '请求错误,未找到该资源', duration: 5, closable: true })
                window.location.href = "/NotFound"
                break;
            case 405:
                Message.warning({ content: '请求方法未允许', duration: 5, closable: true })
                break;
            case 408:
                Message.warning({ content: '请求超时', duration: 5, closable: true })
                break;
            case 500:
                Message.error({ content: '服务器端出错', duration: 5, closable: true })
                break;
            case 501:
                Message.warning({ content: '网络未实现', duration: 5, closable: true })
                break;
            case 502:
                Message.warning({ content: '网络错误', duration: 5, closable: true })
                break;
            case 503:
                Message.warning({ content: '服务不可用', duration: 5, closable: true })
                break;
            case 504:
                Message.warning({ content: '网络超时', duration: 5, closable: true })
                break;
            case 505:
                Message.warning({ content: 'http版本不支持该请求', duration: 5, closable: true })
                break;
            default: {
                if (then) {
                    Message.error({ content: `连接错误${data.msg}`, duration: 5, closable: true })
                } else {
                    Message.error({ content: `连接错误${data.response.status}`, duration: 5, closable: true })
                }
            }
        }
    }
    interceptors (instance, url) {
        // 请求拦截
        instance.interceptors.request.use(config => {
            // 添加全局的loading...
            if (!Object.keys(this.queue).length) {
                // Spin.show() // 不建议开启，因为界面不友好
            }
            this.queue[url] = true
            return config
        }, error => {
            Message.info({ content: 'Tips for manual closing', duration: 10, closable: true });
            return Promise.reject(error)
        })

        // 响应拦截
        instance.interceptors.response.use(res => {
            this.destroy(url)
            const { data, status } = res
            // 1.公共错误处理
            // 2.根据响应码具体处理
            if (data.code == 200) {
                return { data, status };
            }
            //   this.codeMethod(data.code, data, true);
            return { data, status }
        }, error => {
            this.destroy(url)
            //addErrorLog(error.response)

            if (error && error.response) {
                this.codeMethod(error.response.status, error, false);
            } else {
                if (JSON.stringify(error).includes('timeout')) {
                    Message.error('服务器响应超时，请刷新当前页')
                }
            }
            return Promise.reject(error)
        })
    }
    request (options) {
        const instance = axios.create({ timeout: 15 * 1000, withCredentials: false })
        options = Object.assign(this.getInsideConfig(), options)
        this.interceptors(instance, options.url)
        return instance(options)
    }
}
export default HttpRequest
